<template>
  <div>
    <el-card shadow="hover" class="mt-20">
      <el-button
        type="primary"
        :icon="Plus"
        :disabled="!categoryStore.category3Id"
        @click="showAddSpu"
        >添加SPU</el-button
      >
      <el-table :data="spuList" border class="mt-20" v-loading="loading">
        <el-table-column
          label="序号"
          type="index"
          align="center"
          width="80px"
        ></el-table-column>
        <el-table-column prop="spuName" label="SPU名称"></el-table-column>
        <el-table-column prop="description" label="SPU描述"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              type="primary"
              :icon="Plus"
              size="small"
              title="添加SKU"
              @click="showAddSku(row)"
            />
            <el-button
              type="warning"
              :icon="Edit"
              size="small"
              title="修改SPU"
              @click="showUpdateSpu(row)"
            />
            <el-button
              type="info"
              :icon="InfoFilled"
              size="small"
              @click="infoSku(row)"
            />
            <el-button type="danger" :icon="Delete" size="small" />
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="mt-20"
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[3, 6, 9, 12]"
        layout=" prev, pager, next, jumper, ->, sizes, total"
        :total="total"
        @size-change="getSpuList"
        @current-change="getSpuList"
      />

      <el-dialog v-model="dialogUserVisible" :title="skuDesc + '=>  SKU列表 '">
        <el-table :data="skuList">
          <el-table-column label="名称" prop="skuName"> </el-table-column>
          <el-table-column label="价格(元)" prop="price"> </el-table-column>
          <el-table-column label="重量(KG)" prop="weight"> </el-table-column>
          <el-table-column label="默认图片">
            <template v-slot="{ row }">
              <el-image :src="row.skuDefaultImg" />
            </template>
          </el-table-column>
        </el-table>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogUserVisible = false">取消</el-button>
            <el-button type="primary" @click="dialogUserVisible = false">
              确定
            </el-button>
          </span>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>

<script lang="ts">
export default {
  name: "SpuList",
};
</script>

<script lang="ts" setup>
import { ref, watch, defineEmits } from "vue";
import { useCategoryStore } from "@/stores/category";
import { getSpuListApi } from "@/api/product/spu";
import { getFindBySpuIdApi } from "@/api/product/sku";
import { Plus, Edit, Delete, InfoFilled } from "@element-plus/icons-vue";
import type { SpuList, AllSpuItem } from "@/api/product/model/spuModel";
import type { SkuItem, SkuList } from "@/api/product/model/skuModel";
import { useSpuStore } from "@/stores/spu";

const spuStore = useSpuStore();

const currentPage = ref(1);
const pageSize = ref(3);
const total = ref(0);
const spuList = ref<SpuList>([]);
const loading = ref(false);
const dialogUserVisible = ref(false);

const getSpuList = async () => {
  loading.value = true;
  const res = await getSpuListApi({
    page: currentPage.value,
    limit: pageSize.value,
    category3Id: categoryStore.category3Id as number,
  });
  spuList.value = res.records;
  total.value = res.total;
  loading.value = false;
};

const categoryStore = useCategoryStore();
watch(
  () => categoryStore.category3Id,
  () => {
    const { category3Id } = categoryStore;
    if (!category3Id) {
      spuList.value = [];
      //重置分页器
      currentPage.value = 1;
      pageSize.value = 3;
      total.value = 0;
      return;
    }
    getSpuList();
  },
  {
    immediate: true,
  }
);

//点击修改按钮
const showUpdateSpu = (row: AllSpuItem) => {
  spuStore.spuItem = row;
  emit("setIsShow", 2);
};

const emit = defineEmits(["setIsShow"]);
const showAddSpu = () => {
  spuStore.$reset(); //点击重置数据
  emit("setIsShow", 2);
};

const showAddSku = (row: AllSpuItem) => {
  spuStore.spuItem = row;
  emit("setIsShow", 3);
};

const skuList = ref<SkuList>();
const skuDesc = ref();
const infoSku = async (row: SkuItem) => {
  dialogUserVisible.value = true;
  skuList.value = await getFindBySpuIdApi(row.id as number);

  skuList.value.forEach((item) => {
    skuDesc.value = item.skuDesc;
  });
};
</script>

<style scoped></style>
